#!/bin/bash
#
# xvfb          Start up the Xvfb (X11 virtual framebuffer)
#
# chkconfig: 345 95 28
# description: SSH is a protocol for secure remote shell access. \
#              This service starts up the OpenSSH server daemon.
#
# processname: Xvfb
# pidfile: /var/run/Xvfb.pid

### BEGIN INIT INFO
# Provides: Xvfb
# Required-Start: $local_fs $network $syslog
# Required-Stop: $local_fs $syslog
# Should-Start: $syslog
# Should-Stop: $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start up the Xvfb server
# Description:       Xvfb is X Virtual Framebuffer server.
#                    This service starts up the Xvfb server instance.
### END INIT INFO

# default options
RESOLUTION="1024x768x24"
OPTIONS=":5 -screen 0"

# source function library
. /etc/init.d/functions

# pull in sysconfig settings
[ -f /etc/sysconfig/xvfb ] && . /etc/sysconfig/xvfb

RETVAL=0
prog="X Virtual Frame Buffer"
lockfile=/var/lock/subsys/$prog

# Some functions to make the below more readable
XVFB="/usr/bin/Xvfb"
LOG_FILE="/var/log/Xvfb.log"
PID_FILE="/var/run/Xvfb.pid"

rh_status() {
        status -p $PID_FILE $XVFB
}

rh_status_q() {
        rh_status >/dev/null 2>&1
}

start()
{
        [ -x $XVFB ] || exit 5

        echo -n $"Starting $prog: "
        $XVFB $OPTIONS $RESOLUTION >> $LOG_FILE 2>&1 &
        /bin/sleep 1
        echo $! > $PID_FILE
        disown -ar
        rh_status_q && echo_success || echo_failure
        RETVAL=$?
        [ $RETVAL -eq 0 ] && /bin/touch $lockfile
        echo
        return $RETVAL
}

stop()
{
        echo -n $"Stopping $prog: "
        if [ -n "`pidfileofproc $XVFB`" ] ; then
            killproc $XVFB
        else
            failure $"Stopping $prog"
        fi
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && rm -f $lockfile
        return $RETVAL
}

restart() {
	stop
	start
}

case "$1" in
        start)
                rh_status_q && exit 0
                start
                ;;
        stop)
                if ! rh_status_q; then
                        rm -f $lockfile
                        exit 0
                fi
                stop
                ;;
        restart|reload)
                restart
                ;;
        status)
                rh_status
                RETVAL=$?
                [[ $RETVAL -eq 3 && -f $lockfile ]] && RETVAL=2
                ;;
        *)
                echo $"Usage: $0 {start|stop|restart|reload|status}"
                RETVAL=2
esac
exit $RETVAL
